package com.leakey.ddt;


import java.io.IOException;

import javax.xml.xquery.XQException;

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;


@SuppressWarnings("restriction")
public class DDT {
	
	GuideFactory factory = new GuideFactory();
	
	public static void main(String[] args) {
		Display display = new Display();
        Shell shell = new Shell(display, SWT.MIN|SWT.CLOSE);
        shell.setLayout(new GridLayout(1,true));
        shell.setText(Constants.VERSION);
        new DDT().init(shell);
        // 打开shell
        shell.open();
        //始终最前
        /*
         * public static final native boolean SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags)
         */
        OS.SetWindowPos(shell.handle , OS.HWND_TOPMOST, 0 ,0,0,0, SWT.NULL); 
        shell.setAlpha(150);
        shell.setBounds(775, 130,470, 320);
        while (!shell.isDisposed()) {
        	if (!display.readAndDispatch()) {
        		display.sleep();
        	}
        	
        }
	}

	private void init(Shell shell) {
		SashForm sashForm = new SashForm(shell, SWT.NONE);
		Composite left=new Composite(sashForm,SWT.BORDER);
		left.setLayout(new GridLayout(1, true));
		createLeft(left);
		Composite right=new Composite(sashForm,SWT.BORDER);
		right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		right.setLayout(new GridLayout(1, true));
		createRight(right);
		sashForm.setWeights(new int[]{1,1});
		
	}
	
	Text windTxt;
	
	Button following;
	Button against;
	
	private int state = 0;
	
	private Point lp=null;
	private Point rp=null;
	private Point sp=null;
	private Point ep=null;
	Canvas canvas;
	
	private Combo windCombo1;
	private Combo windCombo2;
	private static String[] windData1 = {"0","1","2","3","4"};
	private static String[] windData2 = {"0","1","2","3","4","5","6","7","8","9"};
	private void createRight(final Composite right) {
		canvas = new Canvas(right, SWT.BORDER);
		canvas.setBackground(right.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		//TODO 此处的大小会受分辨率影响，以后再调整
		canvas.setLayoutData(new GridData(200,120));
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				if(lp!=null){
					event.gc.setForeground(right.getDisplay().getSystemColor(SWT.COLOR_GRAY));
					event.gc.drawRoundRectangle(lp.x-3, lp.y-3, 6, 6, 1, 1);
				}
				if(rp!=null){
					event.gc.setForeground(right.getDisplay().getSystemColor(SWT.COLOR_GRAY));
					event.gc.drawRoundRectangle(rp.x-3, rp.y-3, 6, 6, 1, 1);
				}
				if(sp!=null){
					event.gc.setForeground(right.getDisplay().getSystemColor(SWT.COLOR_RED));
					event.gc.drawRoundRectangle(sp.x-3, sp.y-3, 6, 6, 1, 1);
				}
				if(ep!=null){
					event.gc.setForeground(right.getDisplay().getSystemColor(SWT.COLOR_BLUE));
					event.gc.drawRoundRectangle(ep.x-3, ep.y-3, 6, 6, 1, 1);
				}
			}
		});
		canvas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				if(state==1){
					if(lp==null){
						lp = new Point(e.x,e.y);
						state=2;
						System.out.println("小方框左边："+e.x);
					}
				}else if(state==2){
					if(rp==null){
						rp = new Point(e.x,e.y);
						System.out.println("小方框右边："+e.x);
					}
					if(lp!=null&&rp!=null){
						state=3;
					}
					
				}else if(state==3){
						sp = new Point(e.x,e.y);
						state = 4;
						System.out.println("自己位置："+sp);
				}else if(state==4){
						ep = new Point(e.x,e.y);
						state =3;
						System.out.println("敌人位置："+ep);
					
				}
				canvas.redraw();
			}
		});
		new Label(right,SWT.NONE).setText("请选择当前风力");
		Composite windComp = new Composite(right, SWT.BORDER);
		windComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		windComp.setLayout(new GridLayout(3, false));
		Composite windBtnComp = new Composite(windComp, SWT.NONE);
		windBtnComp.setLayout(new RowLayout(SWT.VERTICAL));
		windBtnComp.setLayoutData(new GridData(GridData.FILL_BOTH));
		following = new Button(windBtnComp, SWT.RADIO);
		following.setSelection(true);
		following.setText("顺风");
		against = new Button(windBtnComp, SWT.RADIO);
		against.setText("逆风");
//		windTxt = new Text(windComp,SWT.BORDER);
//		windTxt.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//		initWindTxtListener();
		Composite windComboComp = new Composite(windComp, SWT.NONE);
		windComboComp.setLayout(new GridLayout(3,false));
		windCombo1 = new Combo(windComboComp, SWT.BORDER);
		setComboData(windCombo1,windData1);
		windCombo1.select(0);
		new Label(windComboComp, SWT.NONE).setText(".");
		windCombo2 = new Combo(windComboComp, SWT.NONE);
		setComboData(windCombo2,windData2);
		windCombo2.select(0);
		Button getDataBtn = new Button(windComp, SWT.BORDER);
		getDataBtn.setText("确定");
		getDataBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(lp==null||rp==null||sp==null||ep==null) return;
				// TODO 回车后需要查询数据到左边的表格
				System.out.println("风："+getWind()+" 距离："+getDistance());
				try {
					tv.setInput(factory.getGuides(getDistance(), getWind()));
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (XQException e1) {
					e1.printStackTrace();
				}
				sp=null;
				ep=null;
				canvas.redraw();
			}
		});
		Composite btnComp = new Composite(right, SWT.BORDER);
		btnComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		btnComp.setLayout(new GridLayout(2, false));
		Button newStart = new Button(btnComp,SWT.NONE);
		newStart.setText("新开始");
		newStart.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				state = 1;
				refresh();
				canvas.redraw();
			}
		});
		Button over = new Button(btnComp, SWT.NONE);
		over.setText("结束");
		over.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				refresh();
				state=0;
			}
		});
	}
	private void setComboData(Combo combo, String[] data) {
		if(combo==null) return;
		combo.removeAll();
		for (int i = 0; i < data.length; i++) {
			combo.add(data[i]);
		}
	}

	private void refresh(){
		lp=null;
		rp=null;
		sp=null;
		ep=null;
		canvas.redraw();
	}
	
	public static int MAX_WIND = 5;

	private void createLeft(Composite left) {
		Composite comp = new Composite(left, SWT.NONE);
		comp.setLayout(new FillLayout());
		comp.setLayoutData(new GridData(180,240));
		createTableViewer(comp);
		tv.setContentProvider(new TableViewerContentProvider());
		tv.setLabelProvider(new TableViewerLabelProvider());
		
		final Slider slider = new Slider(left, SWT.NONE);
		slider.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		slider.setMinimum(100);
		slider.setMaximum(255);
		slider.setIncrement(1);
		slider.setSelection(130);
		slider.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(slider.getSelection()>100&&slider.getSelection()<200){
					Display.getCurrent().getActiveShell().setAlpha(slider.getSelection());
				}else{
					e.doit=false;
					return;
				}
			}
		});
	}

	private double getWind() {
		String str1=windCombo1.getText();
		String str2=windCombo2.getText();
		if(str1.matches("^\\d+$")&&str2.matches("\\d+")){
			if(following.getSelection()){
				return Double.parseDouble(str1+"."+str2);
			}else{
				return Double.parseDouble("-"+str1+"."+str2);
			}
		}
		return 0;
	}

	/**
	 * 获取当前我方与敌方的距离
	 * @return ;
	 */
	private double getDistance() {
		if(lp!=null&&rp!=null){
			double p = Math.abs(Arith.div(lp.x-rp.x,10,1));
			System.out.println("一屏距离："+p);
			if(sp!=null&&ep!=null){
				int tmpX = Math.abs(ep.x-sp.x);
				return Arith.div(tmpX, p, 1);
			}
		}
		return 0;
	}

	TableViewer tv;
	
	private void createTableViewer(Composite left) {
		tv = new TableViewer(left, SWT.BORDER|SWT.FULL_SELECTION|SWT.MULTI|SWT.V_SCROLL|SWT.H_SCROLL);
		Table table = tv.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableLayout tLayout = new TableLayout();
		table.setLayout(tLayout);
		tLayout.addColumnData(new ColumnWeightData(15));
		new TableColumn(table, SWT.NONE).setText("名称");
		tLayout.addColumnData(new ColumnWeightData(10));
		new TableColumn(table, SWT.NONE).setText("角度");
		tLayout.addColumnData(new ColumnWeightData(10));
		new TableColumn(table, SWT.NONE).setText("力度");
	}
}
